`<?php

require_once '../library/Utility/Converter.php';

class End_Models_Salebill extends Zend_Db_Table_Abstract {

    protected $_name = 'salebill';
    protected $_primary = 'salebill_id';


    public function _find($id) {
        try {
            $row = parent::find($id)->current();
            if(isset($row))     
                return Utility_Converter::arrayToObject($row->toArray());
            else 
                return $row;
            
        }catch(Zend_Db_Exception $ex) {
            throw $ex;
        }
    }

    public function _filter($stm, $value, $count = null, $offset = null) {
        try {

            $select =  parent::select();

            if(is_array($stm) && is_array($value)) {
                for($i = 0; $i < count($stm); $i++) {
                    $select->where($stm[$i],  $value[$i]);
                }
            }
            else {
                $select->where($stm,  $value);
            }

            if($count != NULL && $offset != NULL) {
                $select->order('id ASC')
                        ->limit($count, $offset);
            }
        
            $rows = parent::fetchAll($select);

            $objects = array();

            for($i = 0; $i < count($rows); $i++) {
                $objects[$i] = Utility_Converter::arrayToObject($rows[$i]->toArray());
            }

            return $objects;
            
        }catch(Zend_Db_Exception $ex) {
            throw $ex;
        }
    }


    public function _update($object) {
        try {

            $arrData = Utility_Converter::objectToArray($object);
            $adapter = parent::getAdapter();
            $where = $adapter->quoteInto('salebill_id = ?', $object->salebill_id);
            parent::update($arrData, $where);
            
        }catch(Zend_Db_Exception $ex) {
            throw $ex;
        }
    }

    public function _insert($object) {
        try {
           
            $object->salebill_id= null; 
            $arrData = Utility_Converter::objectToArray($object); 
            return parent::insert($arrData);
            
        }catch(Zend_Db_Exception $ex) {
            throw $ex;
        }
    }

    public function _delete($id) {
        try {

            $where = parent::getAdapter()->quoteInto('salebill_id = ?', $id);
            return parent::delete($where);
            
        }catch(Zend_Db_Exception $ex) {
            throw $ex;
        }
    }

    //more functions
    
}

